<template>
  <div class="register-container">
    <!-- 顶部蓝底白字导航栏 -->
    <div class="header">
      <span class="logo-text">云进销存</span>
    </div>

    <!-- 注册表单 -->
    <div class="form-container">
      <h2>用户注册</h2>
      <el-form ref="registerForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        
        <el-form-item label="公司名称" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名称"></el-input>
        </el-form-item>
        
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门"></el-input>
        </el-form-item>
        
        <el-form-item label="职员编号" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入职员编号"></el-input>
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        
        <el-form-item prop="agreement">
          <el-checkbox v-model="form.agreement">
            我已阅读并同意<a href="#">《用户协议》</a>和<a href="#">《隐私政策》</a>
          </el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm" class="register-btn">注册</el-button>
          <el-button type="primary" @click="retu" class="register-btn">取消</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 手机验证弹窗 -->
    <el-dialog
      v-model="verifyDialogVisible"
      title="验证手机"
      width="400px"
      :close-on-click-modal="false"
    >
      <div class="verify-dialog">
        <div class="alert-message">
          <i class="el-icon-warning"></i>
          校验码已发送到你的手机，15分钟内输入有效，请勿泄漏
        </div>
        
        <p class="phone-number">手机号码：{{ form.phone }}</p>
        
        <el-form :model="verifyForm" :rules="verifyRules" ref="verifyForm">
          <el-form-item label="校验码" prop="code" required>
            <el-input 
              v-model="verifyForm.code" 
              placeholder="请输入校验码"
              clearable
            >
              <template #append>
                <el-button 
                  :disabled="countdown > 0" 
                  @click="sendCode"
                  class="resend-btn"
                >
                  {{ countdown > 0 ? `重发(${countdown}s)` : '获取验证码' }}
                </el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        
    
        
        <el-button 
          type="primary" 
          @click="confirmVerify" 
          class="verify-btn"
          :loading="verifying"
        >
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'RegisterPage',
  data() {
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: '',
        company: '',
        department: '',
        employeeId: '',
        email: '',
        phone: '',
        agreement: false
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: this.validatePass, trigger: 'blur' }
        ],
        company: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
        department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
        employeeId: [{ required: true, message: '请输入职员编号', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        agreement: [
          { validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请同意用户协议'));
              } else {
                callback();
              }
            }, trigger: 'change' }
        ]
      },
      verifyDialogVisible: false,
      verifyForm: {
        code: ''
      },
      verifyRules: {
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      countdown: 0,
      verifying: false,
      timer: null
    };
  },
  methods: {
    //返回跳转首页
    retu(){
      this.$router.push({
        name: 'Home', 
      });
    },
    validatePass(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    },
    submitForm() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.verifyDialogVisible = true;
          this.sendCode(); // 自动发送验证码
        }
      });
    },
    sendCode() {
      // 模拟发送验证码
      this.countdown = 60;
      this.timer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          clearInterval(this.timer);
        }
      }, 1000);
      
      this.$message.success('验证码已发送');
    },
    confirmVerify() {
      this.$refs.verifyForm.validate(valid => {
        if (valid) {
          this.verifying = true;
          // 模拟验证过程
          setTimeout(() => {
            this.verifying = false;
            this.$message.success('注册成功');
            this.verifyDialogVisible = false;
            // 这里可以跳转到登录页或其他页面
          }, 1500);
        }
      });
    }
  },
  beforeUnmount() {
    clearInterval(this.timer);
  }
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.header {
  background-color: #1f2d3d;
  color: white;
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
}

.form-container {
  width: 600px;
  margin: 30px auto;
  padding: 30px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.register-btn {
  width: 100%;
  margin-top: 20px;
  margin-left: 0px;
}

/* 验证弹窗样式 */
.verify-dialog {
  padding: 0 20px;
}

.alert-message {
  background-color: #fdf6ec;
  color: #e6a23c;
  padding: 12px 15px;
  border-radius: 4px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  font-size: 14px;
}

.alert-message i {
  margin-right: 8px;
  font-size: 16px;
}

.phone-number {
  color: #606266;
  margin: 0 0 20px 0;
  font-size: 14px;
}

.hint-text {
  color: #67c23a;
  font-size: 12px;
  margin: -10px 0 20px 0;
  display: flex;
  align-items: center;
}

.hint-text i {
  margin-right: 8px;
  font-size: 14px;
}

.verify-btn {
  width: 100%;
  background-color: #606266;
  border-color: #606266;
  height: 40px;
  font-size: 14px;
}

.verify-btn:hover {
  background-color: #ff8c39;
  border-color: #ff8c39;
}

.resend-btn {
  padding: 0 15px;
  height: 32px;
}

/* 调整表单元素样式 */
:deep(.el-form-item__label) {
  padding-right: 10px;
  color: #606266;
}

:deep(.el-input-group__append) {
  padding: 0;
  background-color: transparent;
  border-left: none;
}

:deep(.el-input__inner) {
  height: 40px;
}
</style>